package com.hxkcloud.patient.aerotel.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by daipengfei on 16/3/27.
 */
public class ECGDataView extends View {
    private Paint paint;
    private Path path;
    private int availableWidth;
    private int availableHeight;
    int count;
    int counter;
    private int data;

    //float spacing = (float)0.5;
    //float spacing = (float)0.25;
    float spacing = (float) 1.00;

    public ECGDataView(Context context) {
        this(context, null);
    }

    // This constructor is the player but all are necessary!!
    public ECGDataView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setBackgroundColor(Color.TRANSPARENT);
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(Color.BLUE);
        paint.setStrokeWidth(3);
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setStrokeJoin(Paint.Join.ROUND);

        path = new Path();
        counter = 0;
    }

    public ECGDataView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        availableWidth = getMeasuredWidth();
        availableHeight = getMeasuredHeight();
    }

    public void plotProcedure(int data) {
        this.data = data;
        if (counter > availableWidth / spacing) {
            counter = 0;
            path.reset();
        }
        if (counter == 0) {
            path.moveTo(0, availableHeight - data);
        } else {
            path.lineTo(counter * spacing, availableHeight - data);
        }
        counter++;
        // Force redraw
        invalidate();
    }

    @Override
    public void onDraw(Canvas canvas) {
        paint.setTextSize(48);
        canvas.drawPath(path, paint);
    }

}
